package br.com.colegio.carga;

import java.util.HashMap;
import java.util.Map;

import br.com.colegio.util.FileUtil;
import br.com.colegio.util.UtilData;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.tools.remoteapi.RemoteApiInstaller;
import com.google.appengine.tools.remoteapi.RemoteApiOptions;

public class InserirBoletoToCloud
{
	public static void main(String[] args)
	{
//		String username = System.console().readLine("username: ");
//		String password = new String(System.console().readPassword("password: "));
		String username = args[0];
		String password = args[1];

		RemoteApiOptions options = (new RemoteApiOptions())
							.server("sistemamariamorato.appspot.com", 443)
							.credentials(username, password);

		RemoteApiInstaller installer = new RemoteApiInstaller();

		try
		{
			installer.install(options);
			DatastoreService ds = DatastoreServiceFactory.getDatastoreService();

			Query q = new Query("Boleto");
			//q.addFilter("statusBoleto", Query.FilterOperator.EQUAL, "A");

			Map<String, Entity> mB = new HashMap<String, Entity>();
			for (Entity e : ds.prepare(q).asIterable())
				mB.put((String)e.getProperty("codigo"), e);

			System.out.println("RES : " + mB.size());

			int tUpdate2 = 0;
			int tUpdate = 0;
			int tInsert = 0;

			for (String s : FileUtil.pegaURL("e:/_escola/conta-corrente/boletos2012-05-04.csv", "\n").split("\n"))
			{
				String[] c = s.split(";");
				Entity e = mB.get(c[3].replaceAll("'", "").trim());

				if (e == null)
				{
					e = new Entity("Boleto");

					e.setProperty("codigo", c[3].replaceAll("'", "").trim());
					e.setProperty("alunoBoleto", c[1].replaceAll("'", "").trim());
					e.setProperty("grupo", c[2].replaceAll("'", "").trim());
					e.setProperty("dataVencimento", UtilData.parser(c[4].replaceAll("'", "").trim()));
					e.setProperty("dataEmissao", UtilData.parser(c[5].replaceAll("'", "").trim()));
					e.setProperty("valorTotal", Double.valueOf(c[6].replaceAll("'", "").trim().replaceAll(",", ".")));
					e.setProperty("desconto", Double.valueOf(c[7].replaceAll("'", "").trim().replaceAll(",", ".")));
					e.setProperty("multa", Double.valueOf(c[8].replaceAll("'", "").trim().replaceAll(",", ".")));
					e.setProperty("dataMulta", UtilData.parser(c[9].replaceAll("'", "").trim()));
					e.setProperty("jurosDia", Double.valueOf(c[10].replaceAll("'", "").trim().replaceAll(",", ".")));
					e.setProperty("descricao", c[11].replaceAll("'", "").trim());
					e.setProperty("descricaoCompleta", c[12].replaceAll("'", "").trim().replaceAll("\\.\\.", "."));

					if (!c[14].replaceAll("'", "").trim().equals(""))
					{
						e.setProperty("dataPagamento", UtilData.format(UtilData.parser(c[14].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
						e.setProperty("pagamentoValor", Double.valueOf(c[15].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("pagamentoDesconto", Double.valueOf(c[16].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("pagamentoMulta", Double.valueOf(c[17].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("pagamentoTarifa", Double.valueOf(c[18].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("codigoRetornoBoleto", Integer.valueOf(c[13].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("pagamentoCanalBoleto", Integer.valueOf(c[20].replaceAll("'", "").trim()));
						e.setProperty("statusBoleto", (!c[20].replaceAll("'", "").trim().equals("") ? "P" : "A"));
						e.setProperty("pagamentoTipo", "BANCO");

						if (!c[19].replaceAll("'", "").trim().equals(""))
							e.setProperty("creditoData", UtilData.format(UtilData.parser(c[19].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
					}
					else
					{
						e.setProperty("dataPagamento", null);
						e.setProperty("pagamentoValor", null);
						e.setProperty("pagamentoDesconto", null);
						e.setProperty("pagamentoMulta", null);
						e.setProperty("pagamentoTarifa", null);
						e.setProperty("codigoRetornoBoleto", null);
						e.setProperty("pagamentoCanalBoleto", null);
						e.setProperty("statusBoleto", "A");
						e.setProperty("pagamentoTipo", null);
						e.setProperty("creditoData", null);
					}

//					ds.put(e);

					tInsert++;
				}
				else if (!c[20].replaceAll("'", "").trim().equals(""))
				{
					String statusBoleto = (String)e.getProperty("statusBoleto");

					if (statusBoleto == null || statusBoleto.equalsIgnoreCase("A"))
					{
						e.setProperty("alunoBoleto", c[1].replaceAll("'", "").trim());
						e.setProperty("grupo", c[2].replaceAll("'", "").trim());
						e.setProperty("dataVencimento", UtilData.parser(c[4].replaceAll("'", "").trim()));
						e.setProperty("dataEmissao", UtilData.parser(c[5].replaceAll("'", "").trim()));
						e.setProperty("valorTotal", Double.valueOf(c[6].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("desconto", Double.valueOf(c[7].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("multa", Double.valueOf(c[8].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("dataMulta", UtilData.parser(c[9].replaceAll("'", "").trim()));
						e.setProperty("jurosDia", Double.valueOf(c[10].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("descricao", c[11].replaceAll("'", "").trim());
						e.setProperty("descricaoCompleta", c[12].replaceAll("'", "").trim().replaceAll("\\.\\.", "."));

						if (!c[14].replaceAll("'", "").trim().equals(""))
						{
							e.setProperty("dataPagamento", UtilData.format(UtilData.parser(c[14].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
							e.setProperty("pagamentoValor", Double.valueOf(c[15].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoDesconto", Double.valueOf(c[16].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoMulta", Double.valueOf(c[17].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoTarifa", Double.valueOf(c[18].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("codigoRetornoBoleto", Integer.valueOf(c[13].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoCanalBoleto", Integer.valueOf(c[20].replaceAll("'", "").trim()));
							e.setProperty("statusBoleto", (!c[20].replaceAll("'", "").trim().equals("") ? "P" : "A"));
							e.setProperty("pagamentoTipo", "BANCO");

							if (!c[19].replaceAll("'", "").trim().equals(""))
								e.setProperty("creditoData", UtilData.format(UtilData.parser(c[19].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
						}
						else
						{
							e.setProperty("dataPagamento", null);
							e.setProperty("pagamentoValor", null);
							e.setProperty("pagamentoDesconto", null);
							e.setProperty("pagamentoMulta", null);
							e.setProperty("pagamentoTarifa", null);
							e.setProperty("codigoRetornoBoleto", null);
							e.setProperty("pagamentoCanalBoleto", null);
							e.setProperty("statusBoleto", "A");
							e.setProperty("pagamentoTipo", null);
							e.setProperty("creditoData", null);
						}

//						ds.put(e);

						tUpdate++;
					}
				}
				else
				{
					if (!e.hasProperty("statusBoleto"))
					{
						e.setProperty("alunoBoleto", c[1].replaceAll("'", "").trim());
						e.setProperty("grupo", c[2].replaceAll("'", "").trim());
						e.setProperty("dataVencimento", UtilData.parser(c[4].replaceAll("'", "").trim()));
						e.setProperty("dataEmissao", UtilData.parser(c[5].replaceAll("'", "").trim()));
						e.setProperty("valorTotal", Double.valueOf(c[6].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("desconto", Double.valueOf(c[7].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("multa", Double.valueOf(c[8].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("dataMulta", UtilData.parser(c[9].replaceAll("'", "").trim()));
						e.setProperty("jurosDia", Double.valueOf(c[10].replaceAll("'", "").trim().replaceAll(",", ".")));
						e.setProperty("descricao", c[11].replaceAll("'", "").trim());
						e.setProperty("descricaoCompleta", c[12].replaceAll("'", "").trim().replaceAll("\\.\\.", "."));

						if (!c[14].replaceAll("'", "").trim().equals(""))
						{
							e.setProperty("dataPagamento", UtilData.format(UtilData.parser(c[14].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
							e.setProperty("pagamentoValor", Double.valueOf(c[15].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoDesconto", Double.valueOf(c[16].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoMulta", Double.valueOf(c[17].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoTarifa", Double.valueOf(c[18].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("codigoRetornoBoleto", Integer.valueOf(c[13].replaceAll("'", "").trim().replaceAll(",", ".")));
							e.setProperty("pagamentoCanalBoleto", Integer.valueOf(c[20].replaceAll("'", "").trim()));
							e.setProperty("statusBoleto", (!c[20].replaceAll("'", "").trim().equals("") ? "P" : "A"));
							e.setProperty("pagamentoTipo", "BANCO");

							if (!c[19].replaceAll("'", "").trim().equals(""))
								e.setProperty("creditoData", UtilData.format(UtilData.parser(c[19].replaceAll("'", "").trim()), "yyyy-MM-dd HH:mm:ss"));
						}
						else
						{
							e.setProperty("dataPagamento", null);
							e.setProperty("pagamentoValor", null);
							e.setProperty("pagamentoDesconto", null);
							e.setProperty("pagamentoMulta", null);
							e.setProperty("pagamentoTarifa", null);
							e.setProperty("codigoRetornoBoleto", null);
							e.setProperty("pagamentoCanalBoleto", null);
							e.setProperty("statusBoleto", "A");
							e.setProperty("pagamentoTipo", null);
							e.setProperty("creditoData", null);
						}

//						ds.put(e);

						tUpdate2++;
					}
				}

//				ds.put(e);
			}

			System.out.println("-INSERIDOS : " + tInsert);
			System.out.println("-ALTERADOS : " + tUpdate);
			System.out.println("-ALTERADOS 2 : " + tUpdate2);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			installer.uninstall();
		}
	}
}
